#'
#' check whether and which individuals are spatially overlapped, whether is a valide individual coordinate system
#' 
#' 
#' @usage
#' is_overlapped(x)
#' is_overlapped(x,y)
#' 
#' @param x x can be a community or a population, or two vectors contains the coordinate values of individuals
#'
#'@return
#' it returns a FALSE if no individual spatially overlapped, otherwise, a list of overlaped individual groups were returned.
#'
#'
#'@export
is_overlapped=function(x,...){
  UseMethod("is_overlapped")
}

#' @export
is_overlapped.numeric=function(x,y){
  da=cbind(x,y)
  return(anyDuplicated(da)!=0)
}

is_overlapped.data.frame=function(da){
  return(anyDuplicated(as.matrix(da))!=0)
}


#' @export
is_overlapped.community=function(com){
  return(anyDuplicated(com[,c("x","y")])!=0)
}

#' @export
is_overlapped.population=function(pop){
  return(anyDuplicated(pop[,c("x","y")])!=0)
}


is_valid_coordinate=function(x,y,plotdim){
  nx=length(x)
  ny=length(y)
  
  if(nx==0 & ny==0)
    return()
  
  if(nx!=ny){
    stop("length x and y is not equal!")
  }
  
  if(any(is.na(x)) | any(is.na(y))){
    warning("There are missing values in the coordinations of individuals")
  }
  
  xrange=range(x,na.rm=T)
  yrange=range(y,na.rm=T)
  if(any(is.infinite(xrange)) | any(is.infinite(yrange))){
    warning("coordinations of some individuals are infinite")
  }
  
  if(xrange[1]<0 | xrange[2]>plotdim[1] | yrange[1]<0 | yrange[2]>plotdim[2]){
    warning("some individuals distributed outside the plot!")
  }
  
  
  #individuals can't overlap
  if(length(x)!=0 & length(y)!=0){
    if(is_overlapped(x,y)){
      warning("Some individuals are spatilly overlapped. \n see more information about the overlapped individuals in get_overlapped()")
    }
  }
  
  
  
}
